package com.wenhuitec.android.tang.sysui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.wenhuitec.android.tang.R;
import com.wenhuitec.android.tang.sysui.model.PickModel;

import java.util.ArrayList;
import java.util.List;

public class PickAdapter extends RecyclerView.Adapter<PickAdapter.ViewHolder> {

    private List<PickModel> pickModelList = new ArrayList<>();
    private Context context;
    private PickOnClick mPickOnClick;
    private int selected = -1;


    public void setPickModelList(List<PickModel> pickModelList) {
        this.pickModelList = pickModelList;
    }

    public void setSelected(int selected) {
        this.selected = selected;
        if (mPickOnClick != null) {
            if (selected >= 0) {
                mPickOnClick.userClick(pickModelList.get(selected), selected);
            }
        }
        notifyDataSetChanged();
    }
    public void setSelectedNoClick(int selected) {
        this.selected = selected;
        notifyDataSetChanged();
    }
    public void setmPickOnClick(PickOnClick mPickOnClick) {
        this.mPickOnClick = mPickOnClick;
    }

    public PickAdapter(Context context) {
        this.context = context;
    }

    public PickAdapter(Context context, List<PickModel> pickModelList) {
        this.context = context;
        this.pickModelList = pickModelList;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        TextView contextTv;
        LinearLayout pickLv;


        public ViewHolder(View view) {
            super(view);
            contextTv = (TextView) view.findViewById(R.id.contextTv);
            pickLv = view.findViewById(R.id.pickLv);

        }
    }

    public interface PickOnClick {
        void userClick(PickModel pickModel, int position);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_pick, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.contextTv.setText(pickModelList.get(position).getName());

        holder.contextTv.setOnClickListener(v -> {
            if (mPickOnClick != null) {
                mPickOnClick.userClick(pickModelList.get(position), position);
            }
            selected = position;
            notifyDataSetChanged();
        });
        if (selected == position) {
            holder.contextTv.setTextColor(context.getResources().getColor(R.color.white));
            holder.pickLv.setBackground(context.getResources().getDrawable(R.drawable.shape_view_selected_color));
        } else {
            holder.contextTv.setTextColor(context.getResources().getColor(R.color.black));
            holder.pickLv.setBackground(context.getResources().getDrawable(R.drawable.shape_view_color));
        }
    }

    @Override
    public int getItemCount() {
        return pickModelList.size();
    }


}
